package com.tang.portal.component.pay.impl;

import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.tang.common.properties.WxConfigProperties;
import com.tang.common.properties.sys.CoolTeaProperties;
import com.tang.framework.core.cache.CoolTeaConfig;
import com.tang.portal.component.pay.PayService;
import com.tang.portal.component.pay.model.callback.req.base.CallBack;
import com.tang.portal.component.pay.model.create.req.WxCreatePay;
import com.tang.portal.component.pay.model.create.res.WxCreatePayResponse;
import com.tang.portal.component.pay.model.refund.req.WxRefund;
import com.tang.portal.component.pay.model.refund.res.WxRefundResponse;
import com.tang.portal.component.pay.model.withdraw.req.WxWithdraw;
import com.tang.portal.component.pay.model.withdraw.res.WxWithdrawResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.math.BigDecimal;

/**
 * 微信支付
 *
 * @author tang jun liang
 * @Date 2023/6/25 13:36
 */
@Component("wxPayService")
@ConditionalOnProperty(prefix = "cool-tea", name = "wx-pay", havingValue = "true")
public class WxPayServiceImpl implements PayService<WxCreatePay, WxCreatePayResponse,
        WxRefund, WxRefundResponse, WxWithdraw, WxWithdrawResponse, CallBack> {


    @Resource
    @Qualifier(value = "wxPayServiceApi")
    private WxPayService wxPayServiceApi;

    @Resource
    private EntPayService entPayService;

    @Resource
    private CoolTeaProperties coolTeaProperties;



    @SneakyThrows
    @Override
    public WxCreatePayResponse create(WxCreatePay createPay) {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        WxConfigProperties wxConfigProperties = CoolTeaConfig.getConfig(WxConfigProperties.class);
        request.setAppid(wxConfigProperties.getAppId());
        request.setMchId(wxConfigProperties.getMchId());
        request.setOutTradeNo(createPay.getOrderNo());
        //支付回调地址
        request.setNotifyUrl(wxConfigProperties.getNotifyUrl());
        if (coolTeaProperties.getDebug()) {
            request.setTotalFee(100);
        } else {
            request.setTotalFee(createPay.getPayPrice().multiply(BigDecimal.valueOf(100)).intValue());
        }
        request.setOpenid(createPay.getOpenId());
        request.setTradeType(createPay.getTradeType().getValue());
        request.setBody(createPay.getInfo());
        request.setSpbillCreateIp(createPay.getIp());
        WxPayUnifiedOrderResult result = wxPayServiceApi.unifiedOrder(request);
        return new WxCreatePayResponse(result);
    }

    @SneakyThrows
    @Override
    public WxRefundResponse refund(WxRefund refund) {
        WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
        wxPayRefundRequest.setOutRefundNo(refund.getAfterSalesNo());
        wxPayRefundRequest.setTotalFee(refund.getPayPrice().multiply(BigDecimal.valueOf(100)).intValue());
        wxPayRefundRequest.setRefundFee(refund.getAfterSalesPrice().multiply(BigDecimal.valueOf(100)).intValue());
        wxPayRefundRequest.setOutTradeNo(refund.getOrderNo());
        WxPayRefundResult result = wxPayServiceApi.refund(wxPayRefundRequest);
        return new WxRefundResponse(result);
    }

    @SneakyThrows
    @Override
    public WxWithdrawResponse withdraw(WxWithdraw withdraw) {
        EntPayRequest entPayRequest = new EntPayRequest();
        WxConfigProperties wxConfigProperties = CoolTeaConfig.getConfig(WxConfigProperties.class);
        entPayRequest.setAppid(wxConfigProperties.getAppId());
        entPayRequest.setMchId(wxConfigProperties.getMchId());
        entPayRequest.setPartnerTradeNo(withdraw.getWithdrawNo());
        entPayRequest.setOpenid(withdraw.getOpenId());
        entPayRequest.setAmount(BigDecimal.valueOf(100).multiply(withdraw.getPrice()).intValue());
        entPayRequest.setSpbillCreateIp(withdraw.getIp());
        entPayRequest.setDescription(withdraw.getDesc());
        entPayRequest.setCheckName(withdraw.getCheckName());
        EntPayResult result = entPayService.entPay(entPayRequest);
        return new WxWithdrawResponse(result);
    }

    @Override
    public void callBack(CallBack callBack) {

    }
}
